package com.pureut.system.controller;


import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.system.domain.MaterialAwmateria;
import com.pureut.system.domain.MaterialCoding;
import com.pureut.system.domain.SysTheAlarm;
import com.pureut.system.domain.dto.MaterialAwmateriaDto;
import com.pureut.system.domain.vo.MaterialAwmateriaVo;
import com.pureut.system.export.MaterialAwmateriaExport;
import com.pureut.system.service.IMaterialAwmateriaService;
import com.pureut.system.service.IMaterialCodingService;
import com.pureut.system.util.DownTemplateUtil;
import com.pureut.system.util.ExcelUtil;
import com.pureut.system.util.FileConstant;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Description 原料信息
 * @Param
 * @Date 22-11-24
 * @Author hl
 **/
@RestController
@RequestMapping("/awmateria")
public class MaterialAwmateriaController extends BaseController {

    @Resource
    private IMaterialAwmateriaService materialAwmateriaService;

    @Resource
    private IMaterialCodingService materialCodingService;

    /**
     * 获取原料配置
     */
    @RequiresPermissions("material:awmateria:list")
    @GetMapping("/list")
    public TableDataInfo list(MaterialAwmateriaVo materialAwmateria) {
        startPage();
        List<MaterialAwmateriaDto> list = materialAwmateriaService.selectAwmateriaList(materialAwmateria);
        return getDataTable(list);
    }

    /**
     * 新增类型
     */
    @RequiresPermissions("material:awmateria:addType")
    @Log(title = "原料类型", businessType = BusinessType.INSERT)
    @PostMapping("/addType")
    public AjaxResult add(@RequestBody MaterialCoding materialCoding) throws Exception {
        return toAjax(materialCodingService.codingAdd(materialCoding));
    }

    /**
     * 查询单个类型
     */
    @GetMapping("/listCoding")
    public AjaxResult listCoding(long codingId) {
        return AjaxResult.success(materialCodingService.selectListCoding(codingId));
    }

    /**
     * 修改类别
     */
    @RequiresPermissions("material:awmateria:editType")
    @Log(title = "原料类型", businessType = BusinessType.UPDATE)
    @PutMapping("/editType")
    public AjaxResult editType(@RequestBody MaterialCoding materialCoding) throws Exception {
        return toAjax(materialCodingService.updateCodingEdit(materialCoding));
    }

    /**
     * 删除类别
     */
    @RequiresPermissions("material:awmateria:removeType")
    @Log(title = "原料类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeType")
    public AjaxResult remove(long codingId) throws Exception {
        return toAjax(materialCodingService.deleteCodingById(codingId));
    }


    /**
     * 新增原料信息
     */
    @RequiresPermissions("material:awmateria:add")
    @Log(title = "原料信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody MaterialAwmateriaVo materialAwmateria) throws Exception {
        return toAjax(materialAwmateriaService.insertAwmateria(materialAwmateria));
    }

    /**
     * 删除原料信息
     */
    @RequiresPermissions("material:awmateria:remove")
    @Log(title = "原料信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{materialId}")
    public AjaxResult remove(@PathVariable("materialId") String materialId) throws Exception {
        return toAjax(materialAwmateriaService.deleteAwmateriaById(materialId));
    }

    /**
     * 根据编号获取详细信息
     */
    @RequiresPermissions("material:awmateria:query")
    @GetMapping(value = "/{materialId}")
    public AjaxResult getAwmateriaInfo(@PathVariable long materialId) {
        return AjaxResult.success(materialAwmateriaService.selectAwmateriaById(materialId));
    }

    /**
     * 修改原料信息
     */
    @RequiresPermissions("material:awmateria:edit")
    @Log(title = "原料信息", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody MaterialAwmateriaVo materialAwmateria) throws Exception {
        return toAjax(materialAwmateriaService.updateMaterialAwmateriaEdit(materialAwmateria));
    }


    /**
     * 原料冻结
     */
    @RequiresPermissions("material:awmateria:editFreeze")
    @Log(title = "原料信息", businessType = BusinessType.UPDATE)
    @PutMapping("/editFreeze")
    public AjaxResult editFreeze(@RequestBody MaterialAwmateria materialAwmateria) {
        return toAjax(materialAwmateriaService.updateAwmateriaEditFreeze(materialAwmateria));
    }

    /**
     * 原料解冻
     */
    @RequiresPermissions("material:awmateria:editThaw")
    @PutMapping("/editThaw")
    public AjaxResult editThaw(@RequestBody MaterialAwmateria materialAwmateria) {
        return toAjax(materialAwmateriaService.updateAwmateriaEditFreeze(materialAwmateria));
    }

    /**
     * 获取下拉
     */
    @GetMapping("/listThe")
    public AjaxResult listThe(int condingId) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("theDrop", materialCodingService.selectCodingListThe(condingId));
        return ajax;
    }

    /**
     * 原料树形结构展示
     */
    @GetMapping("/listChildren")
    public AjaxResult listChildren() {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("theDrop", materialAwmateriaService.selectChildrenListThe());
        return ajax;
    }


    /**
     * 根据原料编码查询类型
     */
    @GetMapping("/selectCodingId")
    public AjaxResult selectCodingId(String codingId) {
        return AjaxResult.success(materialCodingService.selectCodingId(codingId));
    }

    /**
     * 根据原料类别查询类别名称
     */
    @GetMapping("/selectCodingRulesById")
    public AjaxResult selectCodingRulesById(String codingId) {
        return AjaxResult.success(materialCodingService.selectCodingRulesById(codingId));
    }

    /**
     * 根据物料编码查询详情
     */
    @GetMapping("/selectAllByAlarmMaterialCode")
    public AjaxResult selectAllByAlarmMaterialCode(String materialCode) {
        return AjaxResult.success(materialAwmateriaService.selectAllByAlarmMaterialCode(materialCode));
    }

    /**
     * 根据物料编码获取详情（委外订单详情列表调用 fegin）
     */
    @GetMapping("/getAwmateriaViewByMaterialCode")
    public AjaxResult getAwmateriaViewByMaterialCode(String materialCode) {
        return AjaxResult.success(materialAwmateriaService.getAwmateriaViewByMaterialCode(materialCode));
    }

    /**
     * 原料信息模板下载
     * @param response
     * @throws IOException
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        DownTemplateUtil.downTemplate(response, FileConstant.MATERIAL_AWMATERIA_IMPORT);
    }

    /**
     * 原料信息导入
     */
    @PostMapping("/awmateriaImport")
    @RequiresPermissions("material:awmateria:import")
    @Log(title = "原料信息导入", businessType = BusinessType.INSERT)
    public AjaxResult mouldMessageImport(MultipartFile file) throws Exception {
        ExcelUtil<MaterialAwmateriaExport> excelUtil = new ExcelUtil<MaterialAwmateriaExport>(MaterialAwmateriaExport.class);
        List<MaterialAwmateriaExport> coCustomer = excelUtil.importExcel(file.getInputStream());
        return AjaxResult.success(materialAwmateriaService.importMouldData(coCustomer));
    }

    /**
     * Feign---查询告警信息
     */
    @GetMapping("/getAlarm")
    public SysTheAlarm getAlarm(String materialCode,String type){
        return materialAwmateriaService.getAlarm(materialCode,type);
    }
}